/**
 * 游戏运行界面
 * @author zzhanglin
 */
GameLayer = function()
{
	Sprite.call(this);
	this.mouseChildren = false ;
	
	this._speedX= 0 ; //X方向上的速度
	this._speedY =0 ; //Y方向上的速度
	this._offsetY = 0 ;//游戏屏幕的滚动位移
	
	this._hero ; 
	this._road ;
	this._startFlag = true ;
	this._heroOnFlat=false ;
	
	//添加到舞台上的事件
	this.addEventListener(Event.ADDED_TO_STAGE , this.addedToStage);
};
FLASH.extend(GameLayer , Sprite);
//GameLayer.prototype = new Sprite();
//GameLayer.prototype.constructor = GameLayer ;

/**
 * 当此对象添加到舞台上时
 * @param e
 */
GameLayer.prototype.addedToStage = function(e)
{
	var target = e.getTarget();
	target.removeEventListener(Event.ADDED_TO_STAGE , target.addedToStage);
	target.addEventListener(Event.ENTER_FRAME , target.update );
	//当鼠标在Stage上面按下时执行
	onMouseDownHandler = function(e)
	{
		if(target._heroOnFlat){
			target._speedX = ~target._speedX+1 ; //取反
			if(target._speedX>0) {
				target._hero.right();
			}else{
				target._hero.left();
			}
		}
	};
	target.getStage().addEventListener(MouseEvent.MOUSE_DOWN , onMouseDownHandler );
	
	//添加路
	target._road = new Road();
	target._road.update( );
	target.addChild(target._road );
	
	//添加糖果楼梯
	var LEN = GameSetting.currentLevel.candyArray.length ;
	var candy ;
	var i=0;
	for( i = 0 ; i<LEN ; ++i )
	{
		candy = GameSetting.currentLevel.candyArray[i];
		target.addChild( candy );
		if(candy.direction==2){
			candy.setX( candy.getX()  +candy.getWidth() );
			candy.setScaleX(-1);
		}
	}
	
	//添加礼物
	LEN = GameSetting.currentLevel.giftArray.length ;
	var gift ;
	for( i = 0 ; i<LEN ; ++i )
	{
		gift = GameSetting.currentLevel.giftArray[i];
		target.addChild( gift );
	}
	target._hero = new Hero();
	target.addChild(	target._hero);
};

/**
 * 开始状态
 * @param value
 */
GameLayer.prototype.setStartFlag = function( value)
{
	this._startFlag = value ;
	if(value){
		this._speedY = GameSetting.currentLevel.scrollSpeed ;
		this._speedX = -GameSetting.currentLevel.scrollSpeed;
	}
};

/**
 *  不断执行
 * @param e
 */
GameLayer.prototype.update = function(e)
{
	var target = e.getTarget();
	if(target._startFlag)
	{
		target._offsetY-=target._speedY ;
		//滚动的路
		target._road.update( target._speedY , (GameSetting.SCENE_WIDTH-target._hero.getX())>>1 ); 
		target.updateCandy();
		target.updateGift();
		target.updateHero();
	}
};

/**
 * 更新糖果的位置
 */
GameLayer.prototype.updateCandy = function()
{
	var hitRect ;
	var heroRect = new Rectangle();
	if(this._speedX<0){
		heroRect.x = this._hero.getX() ;
	}else{
		heroRect.x = this._hero.getX()+this._hero.getWidth()-15 ;
	}
	heroRect.y = this._hero.getY()+25 ;
	heroRect.width = 15 ;
	heroRect.height = 30 ;
	
	var candyRect = new Rectangle() ;
	var candy  ;
	var LEN = GameSetting.currentLevel.candyArray.length;
	for( var i=0  ; i<LEN ; ++i)
	{
		candy = GameSetting.currentLevel.candyArray[i];
		candy.setY(candy.getY()+this._speedY);
		if(candy.getY()>GameSetting.SCENE_HEIGHT) {
			candy.visible = false ;
			continue;
		}
		if(candy.visible && candy.getY()+candy.getHeight()>0 && candy.getY()<GameSetting.SCENE_HEIGHT )
		{
			//判断是否在candy上
			if(!hitRect)
			{
				if(candy.direction==1){
					candyRect.x = candy.getX()  ;
				}else{
					candyRect.x = candy.getX() - (candy.getWidth()>>1) ;
				}
				candyRect.y = candy.getY() ;
				candyRect.width = candy.getWidth()>>1;
				candyRect.height = candy.getHeight() ;
				
				if( heroRect.intersects(candyRect)){
					hitRect = candyRect ;
					if(candy.direction==1)
					{
						this._hero.setX( hitRect.x-this._hero.getWidth()+2 );
					}else{
						this._hero.setX( hitRect.x+hitRect.width-2 );
					}
				}
			}
		}
	}
	if(hitRect){
		this._heroOnFlat = true ;
	}else{
		this._heroOnFlat = false ;
	}
};

/**
 * 更新礼物的位置和进行碰撞检测，分数计算
 */
GameLayer.prototype.updateGift = function()
{
	var point = new Point();
	var heroRect = new Rectangle();
	var giftRect = new Rectangle() ;

	var LEN = GameSetting.currentLevel.giftArray.length;
	var gift ;
	for (var i=0 ; i<LEN ; ++i)
	{
		gift =  GameSetting.currentLevel.giftArray[i];
		gift.setY( gift.getY()+this._speedY );
		if(!gift.visible) continue;
		
		if(gift.visible && gift.getY()+gift.getHeight()>0 && gift.getY()<GameSetting.SCENE_HEIGHT )
		{
			point.x = gift.getX() ;
			point.y = gift.getY();
		}
		point.x+=gift.getWidth()*0.5;
		point.y+=gift.getHeight()*0.5;
		
		var dx = point.x - this._hero.getX()-this._hero.getWidth()*0.5 ;
		var dy = point.y - this._hero.getY()-this._hero.getHeight()*0.25 ;
		if( Math.sqrt(dx * dx + dy * dy)<gift.getWidth()*0.5 )
		{
			gift.visible = false;
			GlobalDispatcher.getInstance().dispatchEvent( new ScoreEvent(ScoreEvent.SCORE_CHANGE,10));
		}
	}
};

/**
 * 更新英雄的位置
 */
GameLayer.prototype.updateHero = function()
{
	if(this._speedX!=0 && Math.abs( this._hero.getY()-GameSetting.SCENE_HEIGHT*0.5+50)>2){
		this._hero.setY ( this._hero.getY()+(GameSetting.SCENE_HEIGHT*0.5-this._hero.getY()-50)*0.005 ) ;
	}
	if(this._heroOnFlat)
	{
		if(this._speedX>0) {
			this._hero.right();
		}else{
			this._hero.left();
		}
	} else{
		this._hero.setX( this._hero.getX()+this._speedX ) ; //如果不在平台上才移动
	}
	this.setX( GameSetting.SCENE_WIDTH*0.5- this._hero.getX() );
	if(this.getX()>GameSetting.SCENE_WIDTH*0.4 || this.getX()<-GameSetting.SCENE_WIDTH*0.3){
		this._startFlag = false; //游戏结束
		var candyArr = GameSetting.currentLevel.candyArray ;
		if(this._hero.getY() < candyArr[candyArr.length-1].getY()){
			GlobalDispatcher.getInstance().dispatchEvent( new GlobalEvent(GlobalEvent.GAME_PASS)); //游戏过关
		}else{
			GlobalDispatcher.getInstance().dispatchEvent( new GlobalEvent(GlobalEvent.GAME_OVER)); //游戏失败
		}
	}
};

/**
 * 从舞台上移除时执行
 */
GameLayer.prototype.dispose = function()
{
	Sprite.prototype.dispose.call(this);
	this._hero = null ;
	this._road = null ;
};